// pages/login/login.js
import server from "../../utils/server";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: null,
    time: 60,
    identity_name: '获取验证码',
    disabled: false,
    username: '',
    pic: '',
    sex: '',
    telephone: '',
    phoneCode: '',
    remoteType: server.root_url.indexOf("https://checkorder.mejour.cn") > -1 ? true : false // 是否为远程类型，false为本地，true为远程
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      backgroundColor: '#E9EEF4',
      frontColor: '#000000',
      success: () => {
        console.log('导航栏背景颜色设置成功')
      },
      fail: () => {
        console.log('导航栏背景颜色设置失败')
      },
      complete: () => {
        console.log('导航栏背景颜色设置结束')
      },
    })
    if (options.code) {
      this.setData({
        code: options.code
      })
    } else {
      wx.showToast({
        title: '请授权信息',
        icon: "none",
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWxCode();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取微信code
  getWxCode() {
  },
  
  // 授权手机号登录
  getPhoneNumber(e) {
    let that = this;
    if (e.detail.errMsg === "getPhoneNumber:ok") {
      let param = {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        code: this.data.code,
        deviceId: 1,
      }
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
      server.post("/app/login", param).then(res => {
        if (res.key === 1000) {
          res = res.result;
          wx.setStorageSync('token', res.token);
          wx.setStorageSync('userInfo', res.data);
          wx.setStorageSync('getList', 1);
          wx.hideLoading()
          if(res.data.userType === 5){
            wx.reLaunch({
              url: "/pages/chooseMedium/chooseMedium"
            })
          }else{
            wx.navigateBack({
              delta: 1
            })
          }
        }
      });
    } else {
      wx.showToast({
        title: '授权失败',
        icon: "none"
      })
    }
  },

  getPhoneNumberTestNew(e){
    let param = {
      testUserId: e.currentTarget.dataset.id
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    server.post("/app/loginTestNew", param).then(res => {
      if (res.key === 1000) {
        res = res.result;
        console.log(res.data,123)
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('getList', 1);
        wx.hideLoading()
        if(res.data.userType === 5){
          wx.reLaunch({
            url: "/pages/chooseMedium/chooseMedium"
          })
        }else{
          wx.navigateBack({
            delta: 1
          })
        }
      
      } else {
        wx.showToast({
          title: res.message,
          icon: "none"
        });
      }
    });
  },

  getPhoneNumberTest(){
    let param = {
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    server.post("/app/loginTest", param).then(res => {
      if (res.key === 1000) {
        res = res.result;
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('getList', 1);
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: "none"
        });
      }
    });
  },

  getPhoneNumberTest2(){
    let param = {
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    server.post("/app/loginTest2", param).then(res => {
      if (res.key === 1000) {
        res = res.result;
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('getList', 1);
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      }
    });
  },
  getPhoneNumberTest3(){
    let param = {
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    server.post("/app/loginTest3", param).then(res => {
      if (res.key === 1000) {
        res = res.result;
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('getList', 1);
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      }
    });
  },
  getPhoneNumberTest4(){
    let param = {
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    server.post("/app/loginTest4", param).then(res => {
      if (res.key === 1000) {
        res = res.result;
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('getList', 1);
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      }
    });
  },
  getPhoneNumberTest5(){
    let param = {
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    server.post("/app/loginTest5", param).then(res => {
      if (res.key === 1000) {
        res = res.result;
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('userInfo', res.data);
        wx.setStorageSync('getList', 1);
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      }
    });
  },

})